package boun.center;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

public class BusActivity extends Activity {
	ListView lv;
	BusAdapter ca;
	private int dayType;
	private int direction;
	private String url;
	private String route;
	private ArrayList<String> depPlaces;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bus);
		lv = (ListView) findViewById(R.id.list_schedule);
		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
		Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.routeArray, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adapter);
		spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.directionArray, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner2.setAdapter(adapter2);
		spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener2());
		ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.dayArray, android.R.layout.simple_spinner_item);
		adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner3.setAdapter(adapter3);
		spinner3.setOnItemSelectedListener(new MyOnItemSelectedListener3());
		Button b = (Button) findViewById(R.id.button1);
		b.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new FetchingTask().execute();
			}
			
		});
	}
	
	public class MyOnItemSelectedListener2 implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			direction = pos;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}

	public class MyOnItemSelectedListener3 implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			dayType = pos;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {


		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long arg3) {
			route = parent.getItemAtPosition(pos).toString();
			System.out.println(route);
			Util util = new Util();
			url = util.busScheduleUrl(route);
			depPlaces = new ArrayList<String>();
			if(route.equals("59R")){
				depPlaces.add("Rumeli HisarUstu");
				depPlaces.add("Sisli Merkez");
			}
			else if(route.equals("559C")){
				depPlaces.add("Rumeli Hisarustu");
				depPlaces.add("Taksim");
			}
			else if(route.equals("59RS")){
				depPlaces.add("Sariyer");
				depPlaces.add("Rumeli Hisarustu");
			}
			else if(route.equals("43R")){
				depPlaces.add("Rumeli Hisarustu");
				depPlaces.add("Kabatas");
			}
			Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
			ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(BusActivity.this,android.R.layout.simple_spinner_item,depPlaces);
			adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner2.setAdapter(adapter2);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	}
	
	class FetchingTask extends AsyncTask<Void,Void,BusAdapter>{
		ProgressDialog dialog;
		public FetchingTask(){
			dialog = new ProgressDialog(BusActivity.this);
		}
		@Override
		protected BusAdapter doInBackground(Void... params) {
			
			try {
				ca = new BusAdapter(BusActivity.this,url,direction,dayType,route);
				return ca;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPreExecute(){
			dialog = ProgressDialog.show(BusActivity.this,"","Loading..",true);
		}
		@Override
		protected void onPostExecute(BusAdapter la){
			dialog.dismiss();
			lv.setAdapter(la);
			scrollToCurrentTime();
		}
	}

	public void scrollToCurrentTime() {
		int index = getClosestTimeIndex();
		View v = lv.getChildAt(0);
		int top = (v == null) ? 0: v.getTop();
		lv.setSelectionFromTop(index, top);
	}

	private int getClosestTimeIndex() {
		DateFormat df= new SimpleDateFormat("HH:mm");
		String now = df.format(new Date());
		int position = 0;
		for(;position < lv.getCount();position++){
			String time = (String)lv.getItemAtPosition(position);
			if(now.compareTo(time) == -1)break;
		}
		return position;
	}
}
